$(function() {
$("#searchid").linkbutton({
	 text:'查询',
	 iconCls:'icon-search'
});
	$('#shopermessgerlist').datagrid({
		title : '您所在的位置：商家后台管理&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;商家信息推荐管理',
		idField : 'id',
		nowrap : false,
		striped : true,
		collapsible : false,
		width : 760,
		height : 450,
		url : '../shoperMessage/messageCommandList.shtml',
		frozenColumns : [[{
					field : 'ck',
					title : '选择',
					checkbox : true
				}]],
		columns : [[{
					title : '基本信息',
					colspan : 5
				}, {
					field : 'opt',
					title : '操作',
					width : 200,
					align : 'center',
					rowspan : 2,
					formatter : function(value, rec) {
						if (rec.id == undefined)
							return null;
						else{
									      return '<span style="color:red"><span onclick=Onmodify("'
									+ rec.id
									+ '",1)><font style="cursor:pointer">查看</font></span>&nbsp;&nbsp;<span onclick=tuijian("'
									+ rec.id
									+ '",1)><font style="cursor:pointer">首页推荐</font></span></span>';
							 }
					}
				}], [{
					field : 'title',
					title : '标题',
					width : 170
				}
				,{
					field : 'descrt',
					title : '商家/品牌名称',
					width : 120
				},{
					field : 'creatTimeFormat',
					title : '创建时间',
					width : 120
				},{
					field : 'titleSpe',
					title : '首页推荐',
					align : 'center',
					width : 80,
					formatter:function(value,rec){
						if(value == '1'){
							return "<font color='green'>已推荐</font>";							
						}else
							return "<font color='gray'>未推荐</font>";	
					}
				}
				,{
					field : 'content',
					title : '推荐人',
					align : 'center',
					width : 80
				}

		]],
		pagination : true,
		rownumbers : true,
		toolbar : [  {
					id : 'btnsave',
					text : '删除信息',
					iconCls : 'icon-remove',
					handler : function() {
						var records = $('#shopermessgerlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要删除的记录');
							return;
						}
						if (confirm("确定删除此条信息吗?")) {
						for (var i = 0; i < records.length; i++) {
							if (records[i].id == undefined
									|| records[i].id == "undefined") {

								alert('此条记录已不存在');
								return;
							}
							ids += records[i].id;
							ids += '_';
						}
					
					    deletebyId(ids)

					}
					}
				},{
					id : 'btnadd',
					text : '添加信息',
					iconCls : 'icon-add',
					handler : function() {
					    var brandid = $("#brandid").val();
						$(this).attr("href", '../shoperMessage/toAddMessage.shtml?brandid='+brandid);
						
					 }
					},{
					id : 'btnback',
					text : '返回上一层',
					iconCls : 'icon-back',
					handler : function() {
						$(this).attr("href", '../shoper/branlist.jsp');
					}
				}
				]});
				
				
	$("#modifyWindow").dialog({
		title:'首页推荐设置',
		width:500,
		height:300,
		resizable:false,
		minimizable:false,
		maximizable:false,
		zIndex:100,
		closed:true
	})
	
	$("body").keydown(function(){
	     if(event.keyCode==13)
         {
           onSearch();
         }
	    });
});

   function   onSearch(){
            var queryParams =$('#shopermessgerlist').datagrid("options").queryParams;
			queryParams.title=$("#title").val();
			queryParams.auding=$("#auding").val();
			$('#shopermessgerlist').datagrid("reload");
   }


function Ondelete(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定删除此条信息吗?")) {

		deletebyId(id);
	}
}
function   deletebyId(id)
{
  	var url1 = "../shoperMessage/deleteById.shtml";

						$.ajax({
									url : url1,
									data : "ids=" + id ,
									success : function(msg) {

										var obj = eval('(' + msg + ')');
										var result = obj["result"];
										if(result){alert('删除成功')}
										else{
										  alert('删除失败成功')
										}
										$('#shopermessgerlist').datagrid("reload");

									}
								});
						
}
function Onmodify(id,flag) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if(flag==1){
	window.location.href = "../shoperMessage/findMessageById.shtml?id=" + id+"&see=look" ;
	}else{
	  window.location.href = "../shoperMessage/findMessageById.shtml?id=" + id ;
	}
}

function OnAutding(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定审核此条信息吗?")) {
        var autding=document.getElementById('aud_'+id).value;
		var url1 = "../shoperMessage/updateMessageById.shtml";

						$.ajax({
									url : url1,
									data : "id=" + id+"&autding="+autding ,
									cache:false,
									success : function(msg) {

										var obj = eval('(' + msg + ')');
				                       var result = obj["result"];
			                          if(!result){
                                            alert("审核失败");
                                           return;
						                     }
						                  alert("审核成功");
										$('#shopermessgerlist').datagrid("reload");
									}
								});
	}
}

function tuijian(id){
	$("#modifyWindow").window('open');
	$("#mesid").val(id);
	$.get('../category/category_loadCategory.shtml',function(mes){
			var json = eval('('+mes+')');
			var str = '<select id="bigCategory" name="tgInformation.PcategoryId" onChange="bigCategoryChange()">';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}	
			str+='</select>';
			$('#bigCategory').html(str);
	})
}

function bigCategoryChange(){
	var bigCategoryValue = $("#bigCategory option:selected").val();
	if(bigCategoryValue != ""){
		$('#smallCategory').html('加载中... ...');
		$('#goodsType').html('');
		$.get('../category/category_loadCategoryByPid.shtml?id='+bigCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="tgInformation.categoryId" id="categoryId" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#smallCategory').html(str);
		})
	}
}




function showfilediv() {
	if($('#imgurl').val()!=""){
	   $('#imgdiv').show();
	}
}


function showimg(url){
	 $('#imgdiv').show();
	 $('#img').attr('src',url);
	 $('#imgurl').val(url);
}

function showwhich(obj){
	  if(obj.value==3){
	     $('#trimgdiv').show();
	     $('#information').hide();
	  }
	  if(obj.value==1){
	      $('#information').show();
	       $('#hot2').show();
	       $('#hot1').show();
	  }
	  if(obj.value==2){
	    $('#information').show();
	    $('#hot2').hide();
	    $('#hot1').hide();
	  }
}

function recommend(){
   alert("推荐成功！");
   $('#the2').val("");
   $('#the1').val("");
    $('#mesid').val("");
   $("#modifyWindow").window('close');
   $('#shopermessgerlist').datagrid("reload");
   
}

function showimgdiv(obj){
	   if(obj.value==2){
	     $('#trimgdiv').hide();
	   }else{
	     $('#trimgdiv').show();
	   }
}  



